﻿using System;
using System.Collections.Generic;
using System.Linq;
using MediaSage.Lib.Configuration;

namespace MediaSage.MCClient.PageStack
{
    public class PageStack : IEnumerable<string>
    {
        /// <summary>
        /// May add more to this in the future, holding it as a struct for now
        /// </summary>
        private struct Page
        {
            public string Name { get; set; }
        }

        private Stack<Page> pages = new Stack<Page>();
        private int maxPages;

        public PageStack(int max, Config configuration)
        {
            maxPages = max;
        }

        public void Push(string name)
        {
            pages.Push(new Page() { Name = name });
        }

        public void Pop()
        {
            pages.Pop();
        }

        public int Count
        {
            get { return pages.Count; }
        }

        private List<string> getPages
        {
            get { return pages.Select(i => i.Name).Reverse().ToList(); }
        }

        public override string ToString()
        {
            List<string> pages = getPages;
            int max = maxPages;
            if (pages.Count < maxPages)
            {
                max = pages.Count;
            }
            if (max == 0)
            {
                return String.Empty;
            }
            return (String.Join(" | ", getPages.ToArray(), getPages.Count - max, max));
        }

        public IEnumerator<string> GetEnumerator()
        {
            foreach (var item in pages)
            {
                yield return item.Name;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}